<script lang="ts">
    import { page } from '$app/stores';
    import { Vcs, Client } from '@appwrite.io/console';
    import { onMount } from 'svelte';
    import { VARS } from '$lib/system';

    const client = new Client();
    const vcs = new Vcs(client);

    let installationId: string;
    let repositoryId: string;
    let providerPullRequestId: string;

    let loading = false;
    let error = '';
    let success = '';

    onMount(async () => {
        const endpoint = VARS.APPWRITE_ENDPOINT ?? `${$page.url.origin}/v1`;
        const projectId = $page.url.searchParams.get('projectId');
        client.setEndpoint(endpoint).setProject(projectId).setMode('admin');

        installationId = $page.url.searchParams.get('installationId');
        repositoryId = $page.url.searchParams.get('repositoryId');
        providerPullRequestId = $page.url.searchParams.get('providerPullRequestId') + '';
    });

    async function approveDeployment() {
        if (loading) {
            return;
        }

        loading = true;
        error = '';
        success = '';

        try {
            await vcs.updateExternalDeployments(
                installationId,
                repositoryId,
                providerPullRequestId
            );
            success = 'Deployment approved successfully! Build will start soon.';
        } catch (e) {
            error = e.message;
        } finally {
            loading = false;
        }
    }
</script>

<section class="container" style="display: grid; place-items: center; min-height: 100vh;">
    <div class="u-flex u-flex-vertical u-cross-center" style="width: 100%">
        <div class="card" style="min-width: 600px; max-width: 100%;">
            <h1 class="heading-level-2">Authorize External Deployment</h1>
            <small style="margin-block-start: 8px;display: block;"
                >The deployment for pull request <code class="inline-code"
                    >#{providerPullRequestId}</code> is awaiting approval. When authorized, deployments
                will be started.
            </small>

            <div class="with-borders" style="margin-block-start: 1rem;display: block;">
                <button disabled={loading} on:click={approveDeployment} class="button" type="button"
                    >Approve Deployment</button>
            </div>

            {#if error}
                <p style="margin-block-start: 1rem" class="u-color-text-danger">{error}</p>
            {/if}

            {#if success}
                <p style="margin-block-start: 1rem" class="u-color-text-success">{success}</p>
            {/if}
        </div>
        <div class="u-gap-4 u-flex u-main-center u-cross-center" style="margin-block-start: 2rem;">
            <span class="text">Powered by</span>
            <a
                href="https://appwrite.io/"
                target="_blank"
                style="display: grid;place-items: center;">
                <svg
                    width="132"
                    height="34"
                    viewBox="0 0 132 34"
                    fill="none"
                    xmlns="http://www.w3.org/2000/svg">
                    <path
                        d="M30.3916 17.6788C30.3916 22.216 26.7133 25.8941 22.1756 25.8941C20.0679 25.8941 18.1458 25.1004 16.6913 23.796C19.0212 22.7471 20.6433 20.4053 20.6433 17.6847C20.6433 14.9606 19.0172 12.6164 16.6824 11.5695C18.1381 10.2604 20.0636 9.46356 22.1756 9.46356C26.7133 9.46356 30.3916 13.1416 30.3916 17.6788Z"
                        fill="#A3A8BD" />
                    <path
                        d="M18.8914 11.1213C13.8456 7.30847 6.5638 10.387 5.78442 16.6629C5.42225 19.5797 6.64589 22.4677 8.99326 24.2365C14.0419 28.0459 21.3216 24.9621 22.0967 18.6856C22.4563 15.7725 21.2344 12.8892 18.8914 11.1213V11.1213ZM17.9776 12.3319C22.0943 15.438 21.3042 21.835 16.5556 23.8467C14.352 24.7803 11.8175 24.4674 9.90703 23.0258C5.79061 19.9196 6.58046 13.5226 11.329 11.511C13.5327 10.5776 16.0672 10.8904 17.9776 12.3319Z"
                        fill="#A3A8BD" />
                    <path
                        d="M39.8456 11.6498C40.9732 11.6498 41.9342 11.9416 42.7289 12.5255C43.5236 13.1095 44.07 13.8834 44.3683 14.8473V11.7912H45.7751V22.9159H44.3683V19.8599C44.07 20.8247 43.5236 21.599 42.7289 22.1829C41.9342 22.7668 40.9732 23.0588 39.8456 23.0588C38.8404 23.0588 37.9405 22.8278 37.1457 22.3661C36.3511 21.9045 35.7331 21.2422 35.2915 20.3793C34.8501 19.5166 34.6292 18.5079 34.6292 17.3535C34.6292 16.1858 34.8501 15.1705 35.2915 14.3077C35.7331 13.4457 36.3477 12.7873 37.1357 12.3321C37.9235 11.8771 38.8268 11.6498 39.8456 11.6498ZM40.2123 12.9123C38.9625 12.9123 37.9607 13.3095 37.2064 14.1042C36.4531 14.8989 36.0764 15.982 36.0764 17.3535C36.0764 18.7253 36.4531 19.8085 37.2064 20.6031C37.9607 21.3976 38.9625 21.7948 40.2123 21.7948C41 21.7948 41.7098 21.6117 42.3409 21.245C42.9728 20.8787 43.4687 20.359 43.8286 19.6867C44.1884 19.0143 44.3683 18.2367 44.3683 17.3535C44.3683 16.4705 44.1884 15.6934 43.8286 15.0216C43.4687 14.3495 42.9728 13.8294 42.3409 13.4623C41.7098 13.0956 41 12.9123 40.2123 12.9123ZM54.8213 11.6498C55.8401 11.6498 56.7401 11.8771 57.5211 12.3321C58.3023 12.7873 58.9137 13.4457 59.3552 14.3077C59.7967 15.1705 60.0174 16.1858 60.0174 17.3535C60.0174 18.5079 59.7967 19.5166 59.3552 20.3793C58.9137 21.2422 58.2986 21.9045 57.5111 22.3661C56.7232 22.8278 55.8265 23.0588 54.8213 23.0588C53.6801 23.0588 52.7158 22.7668 51.9279 22.1829C51.14 21.599 50.5969 20.8247 50.2985 19.8599V28.1725H48.8715V11.7912H50.2985V14.8473C50.5969 13.8834 51.14 13.1095 51.9279 12.5255C52.7158 11.9416 53.6801 11.6498 54.8213 11.6498ZM54.4343 12.9123C53.6465 12.9123 52.9366 13.0956 52.3045 13.4623C51.6734 13.8294 51.1812 14.3495 50.8282 15.0216C50.4752 15.6934 50.2985 16.4705 50.2985 17.3535C50.2985 18.2367 50.4752 19.0143 50.8282 19.6867C51.1812 20.359 51.6734 20.8787 52.3045 21.245C52.9366 21.6117 53.6465 21.7948 54.4343 21.7948C55.6975 21.7948 56.7028 21.3976 57.4503 20.6031C58.197 19.8085 58.5702 18.7253 58.5702 17.3535C58.5702 15.982 58.197 14.8989 57.4503 14.1042C56.7028 13.3095 55.6975 12.9123 54.4343 12.9123ZM68.5338 11.6498C69.5527 11.6498 70.4527 11.8771 71.2337 12.3321C72.0149 12.7873 72.6264 13.4457 73.068 14.3077C73.5093 15.1705 73.7302 16.1858 73.7302 17.3535C73.7302 18.5079 73.5093 19.5166 73.068 20.3793C72.6264 21.2422 72.0115 21.9045 71.2237 22.3661C70.4359 22.8278 69.5393 23.0588 68.5338 23.0588C67.3928 23.0588 66.4285 22.7668 65.6407 22.1829C64.8527 21.599 64.3097 20.8247 64.0114 19.8599V28.1725H62.5843V11.7912H64.0114V14.8473C64.3097 13.8834 64.8527 13.1095 65.6407 12.5255C66.4285 11.9416 67.3928 11.6498 68.5338 11.6498ZM68.1472 12.9123C67.3594 12.9123 66.6492 13.0956 66.0173 13.4623C65.3862 13.8294 64.8941 14.3495 64.5409 15.0216C64.1879 15.6934 64.0114 16.4705 64.0114 17.3535C64.0114 18.2367 64.1879 19.0143 64.5409 19.6867C64.8941 20.359 65.3862 20.8787 66.0173 21.245C66.6492 21.6117 67.3594 21.7948 68.1472 21.7948C69.4103 21.7948 70.4156 21.3976 71.1629 20.6031C71.9097 19.8085 72.283 18.7253 72.283 17.3535C72.283 15.982 71.9097 14.8989 71.1629 14.1042C70.4156 13.3095 69.4103 12.9123 68.1472 12.9123ZM89.5209 11.6295H92.2108L89.1746 22.9159H85.9957L83.5714 14.7058L81.0851 22.9159H77.9074L74.8915 11.6295H77.7646L79.5987 20.3491L82.1038 11.6295H85.1399L87.6667 20.3491L89.5209 11.6295ZM96.5102 13.7882C96.9307 13.0687 97.4874 12.5017 98.18 12.0871C98.8735 11.6734 99.6478 11.4666 100.503 11.4666V14.5427H99.6678C97.5627 14.5427 96.5102 15.5205 96.5102 17.476V22.9159H93.6373V11.6295H96.5102V13.7882ZM101.767 8.38884C101.767 7.92721 101.93 7.55055 102.256 7.25894C102.582 6.96675 103.01 6.8205 103.539 6.8205C104.069 6.8205 104.497 6.96675 104.823 7.25894C105.149 7.55055 105.312 7.92721 105.312 8.38884C105.312 8.83718 105.149 9.20749 104.823 9.49993C104.497 9.7922 104.069 9.93846 103.539 9.93846C103.01 9.93846 102.582 9.7922 102.256 9.49993C101.93 9.20749 101.767 8.83718 101.767 8.38884ZM102.093 11.6295H104.966V22.9159H102.093V11.6295ZM112.485 20.4502H113.605V22.9159H112.097C110.833 22.9159 109.866 22.6106 109.193 21.9997C108.521 21.3879 108.185 20.3694 108.185 18.9436V14.0536H106.616V11.6295H108.185V8.8376H111.058V11.6295H113.584V14.0536H111.058V18.984C111.058 19.5274 111.166 19.908 111.384 20.1253C111.601 20.3419 111.968 20.4502 112.485 20.4502ZM126.278 16.8443C126.278 17.1298 126.245 17.456 126.177 17.8224H117.904C117.945 18.8276 118.216 19.5679 118.72 20.0432C119.222 20.5184 119.847 20.7561 120.594 20.7561C121.26 20.7561 121.813 20.5931 122.254 20.2669C122.695 19.9416 122.977 19.5006 123.099 18.9436H126.135C125.986 19.7314 125.667 20.4374 125.179 21.062C124.689 21.6872 124.061 22.1763 123.294 22.5293C122.526 22.8822 121.674 23.0588 120.737 23.0588C119.636 23.0588 118.658 22.8246 117.802 22.3561C116.947 21.8875 116.281 21.2185 115.806 20.3491C115.33 19.4796 115.092 18.454 115.092 17.2727C115.092 16.0906 115.33 15.0618 115.806 14.1864C116.281 13.31 116.947 12.6408 117.802 12.1794C118.658 11.7176 119.636 11.4868 120.737 11.4868C121.85 11.4868 122.825 11.7176 123.66 12.1794C124.495 12.6408 125.141 13.2759 125.596 14.084C126.051 14.892 126.278 15.8121 126.278 16.8443ZM123.385 16.56C123.426 15.6356 123.185 14.9357 122.662 14.4607C122.139 13.9853 121.497 13.7478 120.737 13.7478C119.962 13.7478 119.31 13.9853 118.78 14.4607C118.25 14.9357 117.958 15.6356 117.904 16.56H123.385Z"
                        fill="#A3A8BD" />
                    <path
                        d="M14.1624 14.8366C14.1527 14.8608 14.0268 15.3542 13.8918 15.9395C13.7513 16.5247 13.5288 17.4485 13.4032 17.9949C13.1613 18.9961 13.0161 19.6539 13.0161 19.7409C13.0161 19.7649 13.1662 19.7845 13.3498 19.7845H13.6837L13.8335 19.1169C13.9207 18.7543 14.1143 17.9223 14.2689 17.2694C14.4237 16.6165 14.6122 15.8233 14.6849 15.5039C14.7574 15.1847 14.83 14.8948 14.8445 14.8608C14.859 14.8173 14.7767 14.803 14.5251 14.803C14.3365 14.803 14.172 14.8173 14.1624 14.8366ZM11.565 16.9307L11.1201 17.4145L11.2509 17.5693C11.3232 17.6562 11.5218 17.874 11.6909 18.0531L12.0006 18.3817H12.8809L12.4648 17.932C12.2373 17.6904 12.0487 17.458 12.0487 17.4291C12.0487 17.3952 12.2229 17.1777 12.4359 16.9454C12.6486 16.7086 12.8228 16.5052 12.8228 16.4811C12.8228 16.4616 12.639 16.4472 12.4166 16.4472H12.0151L11.565 16.9307ZM14.9507 16.4762C14.9507 16.4908 15.033 16.5825 15.1345 16.6841C15.512 17.0614 15.7779 17.3808 15.7633 17.4434C15.7538 17.4775 15.5698 17.7047 15.3474 17.9416L14.9461 18.3817H15.3958L15.8456 18.377L16.2565 17.9271C16.4841 17.6758 16.6678 17.453 16.6678 17.4242C16.6678 17.4001 16.4744 17.1727 16.2324 16.9165L15.7975 16.4472H15.3764C15.1394 16.4472 14.9507 16.4616 14.9507 16.4762Z"
                        fill="#A3A8BD" />
                </svg>
            </a>
        </div>
    </div>
</section>
